package com.demomaster.quickcompat.widget.base;

import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.util.Log;

import com.demomaster.quickcompat.drawable.QuickLayerDrawable;
import com.demomaster.quickcompat.drawable.QuickStateListDrawable;


public class QuickDrawableUtil {
    public static String TAG = "QuickDrawableUtil";
    /**
     * 转换为圆角drawable
     *
     * @param drawable
     * @param radius
     * @return
     */
    public static Drawable convertToRadiusDrawable(Drawable drawable, int radius) {
        Log.d(TAG, "convertToRadiusDrawable1=" + drawable);
        return convertToRadiusDrawable(drawable, new float[]{radius, radius, radius, radius, radius, radius, radius, radius});
    }

    public static Drawable convertToRadiusDrawable(Drawable drawable, float[] radii) {
        Log.i(TAG,"convertToRadiusDrawable2=" + drawable);
        if (drawable instanceof GradientDrawable) {
            ((GradientDrawable) drawable).setCornerRadii(radii);
            return drawable;
        } else if (drawable instanceof ColorDrawable) {
            int color = ((ColorDrawable) drawable).getColor();
            int[] colors = new int[]{color, color, color};
            GradientDrawable drawable1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
            drawable1.setCornerRadii(radii);
            return drawable1;
        } else if (drawable instanceof BitmapDrawable) {
            QuickLayerDrawable quickLayerDrawable = new QuickLayerDrawable(new Drawable[]{drawable});
            quickLayerDrawable.setCornerRadii(radii);
            return quickLayerDrawable;
        } else if (drawable instanceof StateListDrawable) {
            StateListDrawable stateListDrawable = (StateListDrawable) drawable;
            QuickStateListDrawable qdRoundRectDrawable = new QuickStateListDrawable();
            // 获取 StateListDrawable 的状态数组
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                int count = stateListDrawable.getStateCount();
                for (int index = 0; index < count; index++) {
                    int[] stateSet1 = stateListDrawable.getStateSet(index);
                    Drawable drawable1 = stateListDrawable.getStateDrawable(index);
                    Log.i(TAG,"drawable1=" + drawable1);
                    qdRoundRectDrawable.addState(stateSet1, convertToRadiusDrawable(drawable1, radii));
                }
                return qdRoundRectDrawable;
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (drawable instanceof RippleDrawable) {
                int[] states = ((RippleDrawable) drawable).getState();
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    // ((RippleDrawable )drawable).setRadius((int) radii[0]);
                }

//                QuickLayerDrawable quickLayerDrawable = new QuickLayerDrawable(new Drawable[]{drawable});
//                quickLayerDrawable.setCornerRadii(radii);
//                for(int index =0;index<states.length;index++) {
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                        quickLayerDrawable.addLayer(((RippleDrawable )drawable).findDrawableByLayerId(states[index]));
//                        //((RippleDrawable )drawable).setRadius((int) radii[0]);
//                    }
//                }
//                ColorDrawable colorDrawable = new ColorDrawable(Color.BLUE);
                return drawable;
            }
        }
        return drawable;
    }
}
